/*
 * Flashlib - QB9 common Flash library
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package com.qb9.flashlib.lang
{
	/**
	 * Pythonic range() function: Return a list of integers that will usually
	 * be iterated with <code>for each</code>.
	 *
	 * <p>Example: To repeat a block of code 10 times, the classic way is:
	 * <pre>
	 * for (var i:int = 0; i &lt; 10; i++) { ... }
	 * </pre>
	 * </p>
	 *
	 * <p>Using range() and coming closer to the Zen:
	 * <pre>
	 * for each (var i:int in range(10)) { ... }
	 * </pre>
	 * </p>
	 */
	public function range(from:int, to:int = -999999, step:int = 0):Array
	{
		if (to == -999999)
		{
			to = from;  
			from = 0;  
		}
		  
		if (step == 0)
			step = from > to ? -1 : 1;  

		var arr:Array = [];
		  
		while (test(from, to, step))
		{
			arr.push(from);
			from += step;
		}
		
		return arr;
	}
}

internal function test(from:int, to:int, step:int):Boolean
{
	if (step > 0)
		return from < to;
	return from > to;
}
